package sg.edu.nus.iss.phoenix.schedule.controller;

import sg.edu.nus.iss.phoenix.authenticate.dao.impl.UserDaoImpl;
import sg.edu.nus.iss.phoenix.authenticate.entity.User;
import sg.edu.nus.iss.phoenix.core.controller.FCUtilities;
import sg.edu.nus.iss.phoenix.radioprogram.entity.RadioProgram;
import sg.edu.nus.iss.phoenix.radioprogram.service.RadioProgramService;
import sg.edu.nus.iss.phoenix.schedule.entity.ProgramSlot;
import sg.edu.nus.iss.phoenix.schedule.service.ScheduleService;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.GregorianCalendar;
import java.util.List;


/**
 * @author tham
 */
@WebServlet(name = "ScheduleController", urlPatterns = {"/ScheduleController/*"})
public class ScheduleController extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    int currentYear = 0;
    int currentWeek = 0;
    ArrayList<ProgramSlot> currentSearchList = new ArrayList<ProgramSlot>();
    GregorianCalendar currentModDate;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String selection = FCUtilities.stripPath(request.getPathInfo())
                .toLowerCase();
        String a = "";
        switch (a) {

        }
        //ArrayList<ProgramSlot> searchList = new ArrayList<ProgramSlot> ();
        ScheduleService schServ = new ScheduleService();

        String strYear = request.getParameter("scheduleYear");
        String strWeek = request.getParameter("scheduleWeek");


        if (strYear != null) {
            currentYear = Integer.parseInt(request.getParameter("scheduleYear"));
        }

        if (strWeek != null) {
            currentWeek = Integer.parseInt(request.getParameter("scheduleWeek"));
        }

        ArrayList<Integer> yearList = new ArrayList<Integer>();
        for (int i = 2010; i < 2016; i++) {
            yearList.add(i);
        }
        request.getSession().setAttribute("yearList", yearList);
        ArrayList<Integer> weekList = new ArrayList<Integer>();
        for (int i = 0; i < 53; i++) {
            weekList.add(i);
        }
        request.getSession().setAttribute("weekList", weekList);
        // ProgramSlot slot = new ProgramSlot();

        //slot.setName( "Presidents Charity");
        // slot.setPresenter( "Dogbert");
        // slot.setProducer("Wally");
        // searchList.add(slot);
        RequestDispatcher rd;
        switch (selection) {
            case "maintainsch":

                rd = request.getRequestDispatcher("/pages/schedule.jsp");
                rd.forward(request, response);
                break;
            case "searchsch":
                ArrayList<ProgramSlot> searchList = schServ.GetProgramSlots(currentYear, currentWeek);
                request.getSession().setAttribute("searchschlist", searchList);
                currentSearchList.clear();
                for (ProgramSlot sl : searchList) {
                    currentSearchList.add(sl);
                }
                rd = request.getRequestDispatcher("/pages/schedule.jsp");
                rd.forward(request, response);
                break;
            case "createsch":
                //  ArrayList<Integer> yearList = new ArrayList<Integer>();
                //  for (int i = 2010; i < 2016; i++)
                //  {
                //     yearList.add(i);
                // }
                //request.getSession().setAttribute("yearList", yearList);
                ArrayList<Integer> monthList = new ArrayList<Integer>();
                for (int i = 1; i < 13; i++) {
                    monthList.add(i);
                }
                request.getSession().setAttribute("monthList", monthList);
                ArrayList<Integer> dayList = new ArrayList<Integer>();
                for (int i = 1; i < 32; i++) {
                    dayList.add(i);
                }
                request.getSession().setAttribute("dayList", dayList);
                ArrayList<Integer> hourList = new ArrayList<Integer>();
                for (int i = 0; i < 24; i++) {
                    hourList.add(i);
                }
                request.getSession().setAttribute("hourList", hourList);

                /*******************Added By Seth*********************************/
                List<User> presenterList = new ArrayList<User>();
                UserDaoImpl userImpl1 = new UserDaoImpl();
                try {
                    presenterList = userImpl1.loadPresenter();
                } catch (SQLException e) {
                    System.out.println(Arrays.toString(e.getStackTrace()));
                }
                ArrayList<String> presenternameList = new ArrayList<String>();
                for (User usr : presenterList) {
                    presenternameList.add(usr.getName());
                }
                request.getSession().setAttribute("presenternameList", presenternameList);

                /****************Added By Seth***********************************/
                List<User> producerList = new ArrayList<User>();
                UserDaoImpl userImpl2 = new UserDaoImpl();
                try {
                    producerList = userImpl2.loadProducer();
                } catch (SQLException e) {
                    System.out.println(Arrays.toString(e.getStackTrace()));
                }

                ArrayList<String> producernameList = new ArrayList<>();

                for (User usr : producerList) {
                    producernameList.add(usr.getName());
                }
                request.getSession().setAttribute("producernameList", producernameList);

                /*********************Added By Seth**********************************/

                ArrayList<String> rpList = new ArrayList<String>();
                RadioProgramService rpServ = new RadioProgramService();
                ArrayList<RadioProgram> rpFind = rpServ.findAllRP();
                for (RadioProgram prog : rpFind) {
                    rpList.add(prog.getName());
                }
                request.getSession().setAttribute("rpProgList", rpList);


                rd = request.getRequestDispatcher("/pages/createschedule.jsp");
                rd.forward(request, response);
                break;
            case "subcreatesch":
                ProgramSlot createPs = new ProgramSlot();
                createPs.setPresenter(request.getParameter("Presenter"));
                createPs.setProducer(request.getParameter("Producer"));
                createPs.setName(request.getParameter("createProgram"));
                String strTest = new String();
                strTest = request.getParameter("createProgram");
                int year, month, day, hour;
                strTest = request.getParameter("createYear");
                year = Integer.parseInt(strTest);
                strTest = request.getParameter("createMonth");
                month = Integer.parseInt(strTest);
                strTest = request.getParameter("createDay");
                day = Integer.parseInt(strTest);
                strTest = request.getParameter("createHour");
                hour = Integer.parseInt(strTest);
                GregorianCalendar myDate = new GregorianCalendar();
                myDate.set(year, month - 1, day, hour, 0, 0);
                createPs.setDate(myDate);
                int i = 0;
                schServ.CreateProgramSlot(createPs);
                //myDate.setHours(Integer.parseInt(request.getParameter("createHour")));
                // myDate.setMinutes(0);
                rd = request.getRequestDispatcher("/ScheduleController/viewall");
                rd.forward(request, response);

                break;
            case "delsch":
                //ArrayList<ProgramSlot> listProg = (ArrayList<ProgramSlot>)request.getSession().getAttribute("searchschlist");
                String strName = request.getParameter("schName");
                for (ProgramSlot sl : currentSearchList) {
                    if (strName.equals(sl.getName())) {
                        GregorianCalendar greCal = sl.getDate();
                        ProgramSlot progDel = new ProgramSlot();
                        progDel.setDate(greCal);
                        progDel.setName(sl.getName());
                        schServ.DeleteProgramSlot(progDel);

                    }

                }

                rd = request.getRequestDispatcher("/ScheduleController/viewall");
                rd.forward(request, response);
                break;

            case "modsch":
                String strName2 = request.getParameter("modName");

                List<ProgramSlot> listGet = schServ.GetAllProgramSlots();
                for (ProgramSlot sl : listGet) {
                    if (strName2.equals(sl.getName())) {
                        GregorianCalendar greCal = sl.getDate();
                        currentModDate = greCal;

                    }

                }

                ArrayList<String> rpList2 = new ArrayList<String>();
                RadioProgramService rpServ2 = new RadioProgramService();
                ArrayList<RadioProgram> rpFind2 = rpServ2.findAllRP();
                for (RadioProgram prog : rpFind2) {
                    rpList2.add(prog.getName());
                }
                request.getSession().setAttribute("rpProgList", rpList2);

                List<User> presenterList2 = new ArrayList<User>();
                UserDaoImpl userImpl3 = new UserDaoImpl();
                try {
                    presenterList2 = userImpl3.loadPresenter();
                } catch (SQLException e) {
                    System.out.println(Arrays.toString(e.getStackTrace()));
                }
                ArrayList<String> presenternameList2 = new ArrayList<String>();
                for (User usr : presenterList2) {
                    presenternameList2.add(usr.getName());
                }
                request.getSession().setAttribute("presenternameList", presenternameList2);

                /****************Added By Seth***********************************/
                List<User> producerList2 = new ArrayList<User>();
                UserDaoImpl userImpl4 = new UserDaoImpl();
                try {
                    producerList2 = userImpl4.loadProducer();
                } catch (SQLException e) {
                    System.out.println(Arrays.toString(e.getStackTrace()));
                }

                ArrayList<String> producernameList2 = new ArrayList<>();

                for (User usr : producerList2) {
                    producernameList2.add(usr.getName());
                }
                request.getSession().setAttribute("producernameList", producernameList2);

                rd = request.getRequestDispatcher("/pages/modifyschedule.jsp");
                rd.forward(request, response);
                break;

            case "submodsch":
                ProgramSlot modProg = new ProgramSlot();
                modProg.setDate(currentModDate);
                String strText = request.getParameter("newName");
                modProg.setName(strText);
                strText = request.getParameter("newPresenter");
                modProg.setPresenter(strText);
                strText = request.getParameter("newProducer");
                modProg.setProducer(strText);
                schServ.ModifyProgramSlot(modProg);
                rd = request.getRequestDispatcher("/ScheduleController/viewall");
                rd.forward(request, response);
                break;

            case "viewall":
                searchList = schServ.GetAllProgramSlots();
                request.getSession().setAttribute("searchschlist", searchList);
                currentSearchList.clear();
                for (ProgramSlot sl : searchList) {
                    currentSearchList.add(sl);
                }
                rd = request.getRequestDispatcher("/pages/schedule.jsp");
                rd.forward(request, response);
                break;
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request  servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException      if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request  servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException      if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
